package edu.berkeley.drivingbuddy;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.series.XYSeries;
import com.androidplot.xy.*;
 
import java.text.DecimalFormat;
import java.util.Arrays;

import edu.berkeley.drivingbuddy.Datastore;
import edu.berkeley.drivingbuddy.ResultObj;
import edu.berkeley.drivingbuddy.ResultsMapItemizedOverlay;
import edu.berkeley.drivingbuddy.SendFriend;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Button;

public class Result  extends MapActivity implements OnClickListener {
    private MapView map;
    private XYPlot score;
    Button mButtonBackToResults;
    Button mButtonToMain;
    Bundle passedInId;
    int recordId;
    ResultObj currentResult;
    private MapController controller;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        
        passedInId =getIntent().getExtras();
        recordId = passedInId.getInt("id");
        currentResult = DrivingBuddy.getDatastore().getResultById(recordId);
        
        mButtonToMain = (Button) findViewById(R.id.button_back_to_main);
        mButtonToMain.setOnClickListener(this);
        
		((TextView) findViewById(R.id.score)).setText("Score: " + currentResult.getScore());
        mButtonBackToResults = (Button) findViewById(R.id.button_send_to_friend);     
        mButtonBackToResults.setOnClickListener(this);
        drawMapView();       
        drawScoreView();
    }
    
    private void drawScoreView() {
    	score = (XYPlot) findViewById(R.id.score_graph);
    	 
        XYSeries series1 = new SimpleXYSeries(
                currentResult.getScoreHistory(),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                "Your route");                             // Set the display title of the series
 
        // Create a formatter to use for drawing a series using LineAndPointRenderer:
        LineAndPointFormatter series1Format = new LineAndPointFormatter(
                Color.rgb(0, 200, 0),                   // line color
                Color.rgb(0, 100, 0),                   // point color
                null);              // fill color (optional)
 
        // Add series1 to the xyplot:
        score.addSeries(series1, series1Format);
 
        // Reduce the number of range labels
        score.setTicksPerRangeLabel(3);
 
        // By default, AndroidPlot displays developer guides to aid in laying out your plot.
        // To get rid of them call disableAllMarkup():
        score.disableAllMarkup();
    }

    private void drawMapView() {
        map = (MapView) findViewById(R.id.results_map);
        map.setSatellite(false);
        map.setBuiltInZoomControls(true);
        controller = map.getController();
        
        List<Overlay> mapOverlays = map.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.images);
        ResultsMapItemizedOverlay itemizedoverlay = new ResultsMapItemizedOverlay(drawable);
        
        Location startLo = currentResult.getGroup().getStartLocation();
        GeoPoint startGe = new GeoPoint((int)(startLo.getLatitude()*1e6), (int)(startLo.getLongitude()*1e6));
        Log.println(1, "StartGe", startGe.toString());
        Location endLo = currentResult.getGroup().getEndLocation();
        GeoPoint endGe = new GeoPoint((int)(endLo.getLatitude()*1e6), (int)(endLo.getLongitude()*1e6));

        OverlayItem overlaystartitem = new OverlayItem(startGe, "Start", "Where you started");
        OverlayItem overlayenditem = new OverlayItem(endGe, "End", "Where you stopped");;
        
        itemizedoverlay.addOverlay(overlaystartitem);
        itemizedoverlay.addOverlay(overlayenditem);
        mapOverlays.add(itemizedoverlay);
        
        controller.animateTo(startGe);
        controller.zoomToSpan((int)startLo.getLatitude(), (int)endLo.getLongitude());
    }
    
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_send_to_friend) {
            startActivity(new Intent(this,SendFriend.class)); 
        }
        else if (v.getId() == R.id.button_back_to_main){
            DrivingBuddy.getMeOutOfHere();
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

}
